package com.wdf.dota;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.umeng.fb.FeedbackAgent;
import com.wdf.rest.service.DotaDiscussionRestClient;

public class AccountActivity   extends Activity{


	Button exitBtn;

	TextView myUsernameView ;
	TextView myMessageTotalView;
	TextView myPostingTotalView;
	
	Button submitlLogin ;
	
	String username;
	String accountId;
	
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.account);
		

		exitBtn =  (Button) findViewById(R.id.submit_comment);
		exitBtn.setOnClickListener(exitBtnListener);
		


		myUsernameView = (TextView)this.findViewById(R.id.my_username);
		myMessageTotalView = (TextView)this.findViewById(R.id.my_message_total);
		myPostingTotalView = (TextView)this.findViewById(R.id.my_posting_total);
		
		SharedPreferences sharedPreferences = getSharedPreferences("wonderful",
				Context.MODE_PRIVATE);
		username = sharedPreferences.getString("username", "");
		accountId = sharedPreferences.getString("accountid", "0");
		myUsernameView.setText(username);
		submitlLogin = (Button)this.findViewById(R.id.submit_login);
		submitlLogin.setOnClickListener(submitlLoginButtonListener);
		
		if("0".equals(accountId)||"".equals(username)){
			myUsernameView.setVisibility(View.GONE);
			submitlLogin.setVisibility(View.VISIBLE);
     		exitBtn.setVisibility(View.GONE);
		}else{
			submitlLogin.setVisibility(View.GONE);
     		exitBtn.setVisibility(View.VISIBLE);
		}
		
	
		ForumPostingRestService service = new ForumPostingRestService();
		service.getTotalMessage();
		service.getForumPostingsTotal();
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	            super.onActivityResult(requestCode, resultCode, data);
	            SharedPreferences sharedPreferences = getSharedPreferences("wonderful",
	    				Context.MODE_PRIVATE);
	    		username = sharedPreferences.getString("username", "");
	    		accountId = sharedPreferences.getString("accountid", "0");
	    		myUsernameView.setText(username);
	    		
	    		if("0".equals(accountId)||"".equals(username)){
	    			myUsernameView.setVisibility(View.GONE);
	    			submitlLogin.setVisibility(View.VISIBLE);
	         		exitBtn.setVisibility(View.GONE);
	    		}else{
	    			myUsernameView.setVisibility(View.VISIBLE);
	    			submitlLogin.setVisibility(View.GONE);
	         		exitBtn.setVisibility(View.VISIBLE);
	         		
	    		}         
	    		
	    		ForumPostingRestService service = new ForumPostingRestService();
	    		service.getTotalMessage();
	    		service.getForumPostingsTotal();
	}
	
	

	
	OnClickListener submitlLoginButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			 Intent intent = new Intent(AccountActivity.this,
        			 LoginActivity.class);
        	  startActivityForResult(intent, 4);
		}
	};
	
	OnClickListener exitBtnListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			 SharedPreferences sharedPreferences = getSharedPreferences("wonderful",
						Context.MODE_PRIVATE);
     		Editor editor = sharedPreferences.edit();
     		editor.remove("username");
     		editor.remove("accountid");
     		editor.remove("accountId");
     		editor.commit();
     		myUsernameView.setVisibility(View.GONE);
     		submitlLogin.setVisibility(View.VISIBLE);
     		exitBtn.setVisibility(View.GONE);
		}
	};
	
	
	OnClickListener returnBtnListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			AccountActivity.this.finish();
		}
	};
	
	public void myHandler(View v){
		switch(v.getId()){
		case R.id.my_postings:
			SharedPreferences sharedPreferences = getSharedPreferences(
					"wonderful", Context.MODE_PRIVATE);
			String accountid = sharedPreferences.getString("accountid", "0");
			String username = sharedPreferences.getString("username", "");

			if ("0".equals(accountid) || username == null
					|| username.length() <= 0) {
				Intent intent = new Intent(AccountActivity.this,
						LoginActivity.class);
				startActivityForResult(intent, 4);
			} else {
				Intent intent = new Intent(AccountActivity.this,
						 MyForumListActivity.class);
	        	  startActivityForResult(intent, 4);
			}
			break;
			

		case R.id.about_productRelativeLayout:
			 Intent intent = new Intent(AccountActivity.this,
					 AboutProductActivity.class);
        	  startActivityForResult(intent, 4);
			break;

		case R.id.about_feedbackRelativeLayout:
			FeedbackAgent agent = new FeedbackAgent(AccountActivity.this);
		    agent.startFeedbackActivity();
			break;
			
		case R.id.my_messages:
			 Intent messageIntent = new Intent(AccountActivity.this,
					 MessageListActivity.class);
       	   startActivityForResult(messageIntent, 4);
			break;
		
		}
		
		
	}
	

	
	class ForumPostingRestService {
        
        
        public ForumPostingRestService(){
        	
        }
		
		 public  void  getTotalMessage(){
			 String url = "/dota/message/total.json?accountId="+accountId;
 			 final RequestParams params = new RequestParams();
			//  params.put("messageId",  messId);
			 DotaDiscussionRestClient.get(url, params, new JsonHttpResponseHandler() {
		            @Override
		            public void onSuccess(JSONObject response) {
		             
		                if(response !=null){
		                	String total = "0";
							try {
								total = response.getString("total");
								myMessageTotalView.setText("我的消息 ("+ total +")");
							} catch (JSONException e) {
								
								e.printStackTrace();
							}
		                	
		                }
		            }
		            
		            public void onFailure(Throwable error){
		                 
		            }
		        });

		 }
		 
		 public  void  getForumPostingsTotal(){

			 String url = "/dota/forumpostings/user/total.json";
 			 final RequestParams params = new RequestParams();
			  params.put("accountId", accountId);

			 DotaDiscussionRestClient.get(url, params, new JsonHttpResponseHandler() {
				 
		            @Override
		            public void onSuccess(JSONObject response) {
		                try {
		                	if(response !=null){
		                  int 	total = response.getInt("total");
		                  myPostingTotalView.setText("我的发贴 ("+total +")");
		                	}
		                } catch (Exception e) {
		                    e.printStackTrace();
		                }finally{
		                	
		                }
		                
		            }
		            

		            
		            public void onFailure(Throwable error){
		            	
		            }
		        });

		 }
}
}
